#include "VideoController.h"

bool VideoController::configAndPlay(VideoPlayInfo& videoPlayInfo) {
    bool result = true;
    result &= setPlayMode(videoPlayInfo);
    result &= setBright(videoPlayInfo);
    result &= setSpeed(videoPlayInfo);
    result &= play(videoPlayInfo);

    mvideoPlayInfo = videoPlayInfo;
    return result;
}


bool VideoController::play(VideoPlayInfo& videoPlayInfo) {

    std::string command{"<AT_CHECK0_VIDEO_"};
    command.append(videoPlayInfo.videoId);
    command.append(1, '>');
    std::string result;
    mchannel.sendStringReturnString(command, result);

    return 0 == result.compare(result.length() - 4, 4, "_OK>");
}



bool VideoController::setSpeed(VideoPlayInfo& videoPlayInfo) {
    std::string command{"<AT_CHECK0_SPEED_"};
    command.append(videoPlayInfo.speed);
    command.append(1, '>');
    std::string result;
    mchannel.sendStringReturnString(command, result);

    return 0 == result.compare(result.length() - 4, 4, "_OK>");
}




bool VideoController::setBright(VideoPlayInfo& videoPlayInfo) {
    std::string command{"<AT_CHECK0_BRIGHT_"};
    command.append(videoPlayInfo.bright);
    command.append(1, '>');
    std::string result;
    mchannel.sendStringReturnString(command, result);

    return 0 == result.compare(result.length() - 4, 4, "_OK>");

}



bool VideoController::setPlayMode(VideoPlayInfo& videoPlayInfo) {
    std::string command{"<AT_CHECK0_MODE_"};
    command.append(videoPlayInfo.mode);
    command.append(1, '>');
    std::string result;
    mchannel.sendStringReturnString(command, result);

    return 0 == result.compare(result.length() - 4, 4, "_OK>");
}



std::string& VideoController::getVideoCount(std::string& result) {

    std::string command{"<AT_CHECK0_VIDEOSUM>"};
    mchannel.sendStringReturnString(command, result);

    result.erase(0, 10);
    std::size_t length = result.size();
    result.resize(length - 1);

    return result;
}



int32_t VideoController::getVideoDuration(VideoPlayInfo& videoPlayInfo) {
    std::string command{"<AT_CHECK0_VIDEOTIME_>"};
    command.append(videoPlayInfo.videoId);
    command.append(1, '>');
    std::string result;
    mchannel.sendStringReturnString(command, result);

    std::size_t length = result.size();
    int32_t duration = 0;
    if (0 == result.compare(length - 4, 4, "_OK>")) {
        duration = std::atoi(&result[11]);
    }

    return duration;
}



bool VideoController::setVideoTime(std::string& datetime, VideoPlayInfo& videoPlayInfo) {

    std::string command = {"<AT_CHECK0_PLAYTIME_"};
    command.append(datetime);
    command.append(1, '_');
    command.append(videoPlayInfo.videoId);
    command.append(1, '>');
    std::string result;
    mchannel.sendStringReturnString(command, result);

    return 0 == result.compare(result.length() - 4, 4, "_OK>");
}



std::string& VideoController::retrieveControllerTime(std::string& result) {
    std::string command{"<AT_CHECK0_SYSTEMTIME>"};
    mchannel.sendStringReturnString(command, result);
    std::size_t length = result.size();
    if (0 == result.compare(length - 4, 4, "_OK>")) {
        result.erase(0, length - 4);
        result.resize(3);
    }
    return result;
}
